package com.wisezone.workflow.web;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.wisezone.common.annotation.WzFunAnnotation;
import com.wisezone.common.annotation.WzWebAnnotation;
import com.wisezone.common.utils.ConstantUtils;
import com.wisezone.workflow.bean.WfDataDictionary;
import com.wisezone.workflow.bean.WfProcessDefinition;
import com.wisezone.workflow.bean.form.WfProcessDefinitionForm;
import com.wisezone.workflow.bean.po.PositionModule;
import com.wisezone.workflow.bean.po.ProcessModule;
import com.wisezone.workflow.bean.search.WfProcessDefinitionSearch;
import com.wisezone.workflow.core.ProcessDefinition;
import com.wisezone.workflow.exception.WfWebException;
import com.wisezone.workflow.utils.ProcessUtils;

/**
 * 流程模板控制类 2010-08-26
 * 
 * @author shan
 * 
 */
@WzWebAnnotation(moduleName = "processDef")
public class WfProcessDefController
		extends
		WfWorkFlowController<WfProcessDefinition, WfProcessDefinitionSearch, WfProcessDefinitionForm> {

	private List<WfDataDictionary> isActiveList;

	private String operate = NEW_EDIT;
	
	public WfProcessDefController() {
	}


	/**
	 * 获得列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String list() throws WfWebException {

		searchEntity = getSearchEntity(true);
		searchEntity.setQueryId("selectFormMaxVer");
		pagination = flowEngine.getWfProcessDefinitionDao().listFormByPage(
				searchEntity);
		isActiveList = this.getDataDictList(20L);
		return LIST;
	}

	/**
	 * 进入新增
	 * 
	 * @return
	 * @throws Exception
	 */
	public String enterNew() throws WfWebException {

		if (id != null) {
			entity = flowEngine.getWfProcessDefinitionDao().load(id);
			entity.setVersion(1);
			entity.setId(null);
		}
		return NEW_EDIT;
	}

	/**
	 * 进入修改
	 * 
	 * @return
	 * @throws Exception
	 */
	public String enterEdit() throws WfWebException {

		entity = flowEngine.getWfProcessDefinitionDao().load(id);
		return NEW_EDIT;
	}

	/**
	 * 进入升级
	 * 
	 * @return
	 * @throws WfWebException
	 */
	public String enterUpgrade() throws WfWebException {

		entity = flowEngine.getWfProcessDefinitionDao().load(id);
		entity.setId(null);
		String fileContent = entity.getFileContent();
		JSONObject jsonObject = new JSONObject(fileContent);
		JSONObject templateForm = jsonObject.getJSONObject("form");
		Integer version = entity.getVersion();
		templateForm.set("version", version + 1);
		entity.setFileContent(jsonObject.toString());
		return NEW_EDIT;
	}

	/**
	 * 进入查看
	 * 
	 * @return
	 * @throws Exception
	 */
	public String enterView() throws WfWebException {

		entity = flowEngine.getWfProcessDefinitionDao().load(id);
		String processInstId = request.getParameter("processInstId");
		if(StringUtils.isNotEmpty(processInstId)){
			List<PositionModule> positionList = flowEngine.getPositions(Long.valueOf(processInstId));
	        this.model.put("positionList", positionList);
		}
		operate = VIEW;
		return VIEW;
	}

	/**
	 * 保存
	 * 
	 * @return
	 * @throws Exception
	 */
	public String save() throws WfWebException {

		if (id != null) {
			return update();
		}
		entity = getEntity();

		// 数据校验

		entity.setState(1);
		Long isActive = getStateId("processDefinitionIsActive", "1");
		entity.setIsActive(isActive);

		try {
			this.flowEngine.save(entity);
		} catch (Exception e) {

			this.addErrMsg("流程模板保存失败", NEW_EDIT);
			throw new WfWebException(e);
		}
		return NEW_EDIT;
	}

	/**
	 * 修改
	 * 
	 * @return
	 * @throws Exception
	 */
	public String update() throws WfWebException {

		entity = getEntity();
		this.flowEngine.update(id, entity.getFileContent());
		return NEW_EDIT;
	}

	/**
	 * 发布
	 * 
	 * @return
	 * @throws WfWebException
	 */
	public String publish() throws WfWebException {

		this.flowEngine.publish(id);
		return getRedirect(LIST);
	}

	/**
	 * 激活
	 * 
	 * @return
	 * @throws WfWebException
	 */
	public String enabled() throws WfWebException {

		ProcessDefinition processDef = this.flowEngine.loadProcessDefById(id);
		processDef.enabled();
		return getRedirect(LIST);
	}

	/**
	 * 禁用
	 * 
	 * @return
	 * @throws WfWebException
	 */
	public String disabled() throws WfWebException {

		ProcessDefinition processDef = this.flowEngine.loadProcessDefById(id);
		processDef.disabled();
		return getRedirect(LIST);
	}

	/**
	 * 删除
	 * 
	 * @return
	 * @throws Exception
	 */
	public String delete() throws WfWebException {

		Collection<Serializable> idList = getIdList();
		if (idList != null) {
			flowEngine.getWfProcessDefinitionDao().deleteAllById(idList);
		}
		return getRedirect(LIST);
	}

	/**
	 * json 格式 转 xml 格式
	 * 
	 * @param jsonStr
	 * @return
	 * @throws Exception
	 */
	@WzFunAnnotation(act = ConstantUtils.EXEC_AJAX)
	public String jsonToXml() throws WfWebException {

		try {
			searchEntity = getSearchEntity();
			ProcessModule processModule = ProcessUtils.jsonToProcess(searchEntity.getFileContent());
			String xmlStr = ProcessUtils.processToXml(processModule);
			return xmlStr;
		} catch (Exception e) {
			throw new WfWebException("json 格式转 xml 格式失败", e);
		}
	}

	@WzFunAnnotation(act = ConstantUtils.EXEC_AJAX)
	public List<WfDataDictionary> getIsActiveList() {
		return isActiveList;
	}

	public String getOperate() {
		return operate;
	}

}
